<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="楼栋" prop="buildingName">
                <el-select v-model="queryParams.buildingName" placeholder="请选择楼栋" style="width:200px">
                    <el-option v-for="dict in dict.type.loudong" :key="dict.value" :label="dict.label"
                        :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="宿舍编号" prop="dormitorycode">
                <el-input v-model="queryParams.dormitorycode" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="抄表时间" prop="readDate">
                <!-- <el-date-picker clearable v-model="queryParams.readDate" type="month" value-format="yyyy-MM"
                    placeholder="请选择抄表时间">
                
                </el-date-picker> -->
                <el-date-picker v-model="queryParams.selectDate" type="datetimerange" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期">
                    </el-date-picker>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>


                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['business:degrees:export']">导出</el-button>

            </el-form-item>
        </el-form>


        <el-table v-loading="loading" :data="degreesList" @selection-change="handleSelectionChange" border height="800">
            <el-table-column label="楼栋" align="center" prop="buildingName" />
            <el-table-column label="宿舍编号" align="center" prop="dormitorycode" />
            <el-table-column label="性质" align="center" prop="nature" />
            <el-table-column label="上次抄表时间" align="center" prop="lastReadDate">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.lastReadDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="最近抄表时间" align="center" prop="readDate">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.readDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="抄表度数" align="center" prop="numberdegrees" />


            <el-table-column label="使用度数" align="center" prop="difference" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:degrees:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['business:degrees:remove']">删除最近记录</el-button>
                    <el-button size="mini" type="text" icon="el-icon-date" @click="handleHis(scope.row)"
                        v-hasPermi="['business:degrees:his']">历史</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改宿舍电度数对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <!-- <el-form-item label="宿舍id" prop="dormitoryid">
                    <el-input v-model="form.dormitoryid" placeholder="请输入宿舍id" />
                </el-form-item> -->
                <el-form-item label="抄表时间" prop="readDate">
                    <el-date-picker clearable v-model="form.readDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择抄表时间" disabled>
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="度数" prop="numberdegrees">
                    <el-input v-model="form.numberdegrees" placeholder="请输入度数" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 抄表历史 -->
        <el-dialog :title="histitle" :visible.sync="hisopen" width="800px" append-to-body>
            <el-table v-loading="hisloading" :data="hisdegreesList" border>
                <el-table-column label="抄表时间" align="center" prop="readDate">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.readDate, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="度数" align="center" prop="numberdegrees" />

                <el-table-column label="上次抄表时间" align="center" prop="lastReadDate">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.lastReadDate, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="差值" align="center" prop="difference" />
                <el-table-column label="备注" align="center" prop="remark" />
                <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="['business:degrees:edit']">修改</el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['business:degrees:remove']">删除</el-button>
                        <el-button size="mini" type="text" icon="el-icon-date" @click="handleHis(scope.row)"
                            v-hasPermi="['business:degrees:his']">历史</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="histotal > 0" :total="histotal" :page.sync="hisqueryParams.pageNum"
                :limit.sync="hisqueryParams.pageSize" @pagination="getHisList" />
        </el-dialog>
    </div>
</template>
  
<script>
import { listDegrees, getDegrees, delDegrees, addDegrees, updateDegrees, listHisDegrees } from "@/api/business/degrees";
import moment from 'moment';
export default {
    name: "Degrees",
    dicts: ['loudong'],
    data() {
        return {
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 宿舍电度数表格数据
            degreesList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 50,
                buildingName: null,
                selectDate: null,
                startreaddate:null,
                endreaddate:null
 
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {

                dormitoryid: [
                    { required: true, message: "宿舍id不能为空", trigger: "blur" }
                ],
                readDate: [
                    { required: true, message: "抄表时间不能为空", trigger: "blur" }
                ],
                numberdegrees: [
                    { required: true, message: "度数不能为空", trigger: "blur" }
                ]
            },
            // 遮罩层
            hisloading: false,
            // 弹出层标题
            histitle: "",
            // 是否显示弹出层
            hisopen: false,
            // 宿舍电度数表格数据
            hisdegreesList: [],
            histotal: 0,
            // 查询参数
            hisqueryParams: {
                pageNum: 1,
                pageSize: 10,
                dormitoryid: null,

            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询宿舍电度数列表 */
        getList() {
            if (this.queryParams.selectDate != null) {
                this.queryParams.startreaddate= moment(this.queryParams.selectDate[0]).format('YYYY-MM-DD');
                this.queryParams.endreaddate= moment(this.queryParams.selectDate[1]).format('YYYY-MM-DD');
            }
            console.log(this.queryParams)
            this.loading = true;
            listDegrees(this.queryParams).then(response => {
                this.degreesList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                dormitoryid: undefined,
                readDate: undefined,
                numberdegrees: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                buildingName: null,
                selectDate: null,
                startreaddate:null,
                endreaddate:null
            },
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加宿舍电度数";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const id = row.id || this.ids
            getDegrees(id).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = "修改宿舍电度数";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.form.id != null) {
                        updateDegrees(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        addDegrees(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除宿舍电度数编号为"' + ids + '"的数据项？').then(() => {
                this.loading = true;
                return delDegrees(ids);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/degrees/export', {
                ...this.queryParams
            }, `degrees_${new Date().getTime()}.xlsx`)
        },
        handleHis(row) {
            this.hisopen = true;
            this.shititle = "历史记录";
            this.hisqueryParams.dormitoryid = row.dormitoryid;
            this.getHisList();
        },
        getHisList() {
            this.hisloading = true;
            listHisDegrees(this.hisqueryParams).then(response => {
                this.hisdegreesList = response.rows;
                this.histotal = response.total;
                this.hisloading = false;
            });
        }

    }
};
</script>
  